﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HardwareAs
{
    public partial class ManageProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            DataSBind();

        }
        private void DataSBind()
        {
            HardwareLinqDataContext nb = new HardwareLinqDataContext();
            ListView1.DataSource = nb.SelectAllProduct();
           
            ListView1.DataBind();
           
    
        }

        /*
         * EditItemtemplete will response and show out
         * */
        protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            ListView1.InsertItemPosition = InsertItemPosition.None;
         (sender as ListView).EditIndex=   e.NewEditIndex;
          DataSBind();
           
        }
        /*
         *  StoreProceDure InsertProduct
            */
        protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            string id = (ListView1.InsertItem.FindControl("txtID") as Label).Text;
            string Title = (ListView1.InsertItem.FindControl("txtTitle") as TextBox).Text;
            string CID = (ListView1.InsertItem.FindControl("DropDownList1") as DropDownList).SelectedValue;
            string tagline = (ListView1.InsertItem.FindControl("txtTagLine") as TextBox).Text;
            string desc = (ListView1.InsertItem.FindControl("txtDescription") as TextBox).Text;
            string keyword = (ListView1.InsertItem.FindControl("txtKeywords") as TextBox).Text;
            string imageurl = (ListView1.InsertItem.FindControl("txtImageUrl") as TextBox).Text;

            HardwareLinqDataContext db = new HardwareLinqDataContext();
            db.InsertProduct(Title, tagline, desc, Convert.ToInt32(CID), imageurl, keyword);
            ListView1.InsertItemPosition = InsertItemPosition.None;
            DataSBind();
            
        }

        protected void ListView1_ItemInserted(object sender, ListViewInsertedEventArgs e)
        {
           
        }

        protected void Button1_Click(object sender, EventArgs e) //Open Insert Mode
        {
            ListView1.InsertItemPosition = InsertItemPosition.FirstItem;
            DataSBind();
        }

        protected void DropDownList2_Load(object sender, EventArgs e)// Bind with all category
        {

            (sender as DropDownList).DataTextField = "CategoryName";
            (sender as DropDownList).DataValueField = "Id";
            HardwareLinqDataContext nb = new HardwareLinqDataContext();
            (sender as DropDownList).DataSource = nb.SelectAllCategory();
            
            (sender as DropDownList).DataBind();
        }

 
        protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            ListView1.EditIndex = -1;
            DataSBind();
        }

        protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            string id = (ListView1.EditItem.FindControl("txtID") as Label).Text;
            string Title = (ListView1.EditItem.FindControl("txtTitle") as TextBox).Text;
            string CID = (ListView1.EditItem.FindControl("DropDownList1") as DropDownList).SelectedValue;
            string tagline = (ListView1.EditItem.FindControl("txtTagLine") as TextBox).Text;
            string desc = (ListView1.EditItem.FindControl("txtDescription") as TextBox).Text;
            string keyword = (ListView1.EditItem.FindControl("txtKeywords") as TextBox).Text;
            string imageurl = (ListView1.EditItem.FindControl("txtImageUrl") as TextBox).Text;
            
            HardwareLinqDataContext db = new HardwareLinqDataContext();
            db.UpdateProduct(Convert.ToInt32(id), Title, tagline, desc, Convert.ToInt32(CID), imageurl, keyword);
        }

        protected void ListView1_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
        {
            ListView1.EditIndex = -1;
            DataSBind();
        }

        protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {

       
        }
        protected void DeleteProduct(object sender, EventArgs e)//StoreProcedure DeleteProduct
        {
            HardwareLinqDataContext db = new HardwareLinqDataContext();
            db.DeleteProduct(Convert.ToInt32((sender as Button).CommandArgument));
            DataSBind();
        }





    }
}